home *** CD-ROM | disk | FTP | other *** search
/ Amiga Game-Power / Amiga Game-Power.iso / pd mix ii / access / thai / random.c < prev    next >
C/C++ Source or Header  |  1994-05-20  |  1KB  |  63 lines

  1. #include "quiz.h"
  2.  
  3. extern struct thai_phrase *random ();
  4.  
  5.  
  6. random_sentence ()
  7. {
  8.     chosen_sentence = random ( &sentence_head );
  9. }
  10.  
  11.  
  12. random_word ()
  13. {
  14.     chosen_word = random ( &word_head );
  15. }
  16.  
  17.  
  18. static
  19. struct thai_phrase *
  20. random ( head )
  21. struct thai_phrase *head;
  22. {
  23.     long dist ();
  24.  
  25.     long count;
  26.     long total_right;
  27.     long total_wrong;
  28.     long num;
  29.     static long seed = 0;
  30.     struct thai_phrase *p;
  31.     ULONG seconds , micros;
  32.  
  33.     if ( head->next == NULL )
  34.         return ( head );
  35.     total_right = 0;
  36.     total_wrong = 0;
  37.     count = 0;
  38.     for ( p = head->next; p != NULL; p = p->next ) {
  39.         total_right += p->right;
  40.         total_wrong += p->wrong;
  41.         count++;
  42.     }
  43.     CurrentTime ( &seconds , µs );
  44.     seed += seconds + micros + 1234567;
  45.     num = ( seed & 0x7FFF ) % ( count + dist ( total_wrong , total_right ) );
  46.     for ( p = head->next; p != NULL; p = p->next ) {
  47.         num -= dist ( (long)p->wrong , (long)p->right ) + 1;
  48.         if ( num < 0 )
  49.             return ( p );
  50.     }
  51.     return ( head->next );
  52. }
  53.  
  54.  
  55. static long
  56. dist ( wrong , right )
  57. long wrong , right;
  58. {
  59.     if ( wrong > right )
  60.         return ( wrong - right );
  61.     return ( 0 );
  62. }
  63.